package com.examples.AppoointmentManager;

import android.content.ActivityNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

//class displaying the map using latitude and longitude
public class ShowOnMapActivity extends MapActivity{
	
	MapView mapView; 
    MapController mc;
    GeoPoint p;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        
        try {	
        	//creating Bundle reference and assigning previous Intent 
        Bundle extras = getIntent().getExtras();
		if (extras == null) {
			return;
		}
		
		//retrieving values from Intent 
		String lats=extras.getString("lat");
		String lngs=extras.getString("lng");
		String map_view=extras.getString("map_view");
		
		//creating reference for mapView and assigning it to ID themap
		mapView =(MapView) findViewById(R.id.themap);
		//setting the Zoom Controls
		mapView.setBuiltInZoomControls(true);
        mc = mapView.getController();
        //creating the marker to pint the given location
        Drawable marker=getResources().getDrawable(android.R.drawable.star_big_on);
        int markerWidth = marker.getIntrinsicWidth();
        int markerHeight = marker.getIntrinsicHeight();
        marker.setBounds(0, markerHeight, markerWidth, 0);
        MyItemizedOverlay myItemizedOverlay = new MyItemizedOverlay(marker);
        mapView.getOverlays().add(myItemizedOverlay);
        
        double lat = Double.parseDouble(lats);
        double lng = Double.parseDouble(lngs);
        
        //condition for displaying the map in different views
        if(map_view.equals("StreetView"))
        mapView.setStreetView(true);
        else if(map_view.equals("SatelliteView"))
        mapView.setSatellite(true);
        else if(map_view.equals("TrafficView"))
        mapView.setTraffic(true);
        
        //creating GeoPoint which stores a pair of latitude and longitude
        p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));
        //Start animating the map towards the given points
        mc.animateTo(p);
        myItemizedOverlay.addItem(p, "myPoint1", "myPoint1");
        mc.setZoom(17); 
        mapView.invalidate();
    	}
        catch ( ActivityNotFoundException e) {
        	//Exception thrown when Activity can not be found because one of its variants failure
		    e.printStackTrace();
		}
    	catch(NullPointerException e) {
			Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
			e.printStackTrace();
		}
    	catch (Exception e) {
			e.printStackTrace();
		}
    }
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
}
